{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "af6c35bf",
   "metadata": {},
   "source": [
    "# 浙教必修二 4.2搭建信息系统\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9821ea3",
   "metadata": {},
   "source": [
    "> 行空板本身相当于一台能接入开源硬件的linux电脑，因此可以将服务器端及传感器数据发送都在行空板上使用纯Python代码来实现。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a4b7bef",
   "metadata": {},
   "source": [
    "# 使用的技术点"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "049dc876",
   "metadata": {},
   "source": [
    "- flask库实现web服务器的搭建\n",
    "- sqlite库实现数据库的操作\n",
    "- request库实现数据的传输\n",
    "- pinpong库实现传感器数据的读取\n",
    "- unihiker库实现行空板屏幕的显示"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4bfcded",
   "metadata": {},
   "source": [
    "# 操作步骤"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e23d09ec",
   "metadata": {},
   "source": [
    "## 3-发送数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef79dc03",
   "metadata": {},
   "source": [
    "保持服务器端代码运行中，运行下面代码，将数据发送到服务器网页上显示\n",
    "> 注此处使用P29光线传感器替代温度数据，实际需要连接一个LM35到模拟口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9c73954",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  ___________________________\n",
      " |                           |\n",
      " |      PinPong v0.4.9       |\n",
      " |    Designed by DFRobot    |\n",
      " |___________________________|\n",
      " \n",
      "[01] Python3.7.3 Linux-4.4.143-67-rockchip-g01bbbc5d1312-aarch64-with-debian-10.11 Board: UNIHIKER\n",
      "selected -> board: UNIHIKER serial: /dev/ttyS3\n",
      "[10] Opening /dev/ttyS3\n",
      "[32] Firmata ID: 3.3\n",
      "[22] Arduino compatible device found and connected to /dev/ttyS3\n",
      "[40] Retrieving analog map...\n",
      "[42] Auto-discovery complete. Found 30 Digital Pins and 30 Analog Pins\n",
      "------------------------------\n",
      "All right. PinPong go...\n",
      "------------------------------\n",
      "\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import requests\n",
    "import time\n",
    "from pinpong.board import Board,Pin\n",
    "from pinpong.extension.unihiker import *\n",
    "from unihiker import GUI\n",
    "\n",
    "IP=\"10.1.2.3\"\n",
    "PORT=\"8000\"\n",
    "\n",
    "u_gui=GUI()\n",
    "u_gui.draw_text(text=\"访问结果：\",x=10,y=10)\n",
    "tx_status=u_gui.draw_text(text=\"\",x=10,y=50)\n",
    "u_gui.draw_text(text=\"数据：\",x=10,y=100)\n",
    "tx_value=u_gui.draw_text(text=\"\",x=10,y=150)\n",
    "\n",
    "Board().begin()\n",
    "p_p29_analog=Pin(Pin.P29, Pin.ANALOG) #P29 光线传感器\n",
    "\n",
    "\n",
    "\n",
    "while True:\n",
    "    val = p_p29_analog.read_analog()\n",
    "    tx_value.config(text=str(val))\n",
    "    resp = requests.get(\"http://\"+IP+\":\"+PORT+\"/input?id=1&val=\" + str(val) )\n",
    "    if resp.status_code == 200:\n",
    "        tx_status.config(text=\"访问成功\")\n",
    "    else:\n",
    "        tx_status.config(text=\"访问错误\")\n",
    "    time.sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3b340c7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59201fe6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
